package dao.impl;

import dao.PhoneDAO;
import entity.Phone;
import util.JdbcUtil;

import java.sql.SQLException;
import java.util.List;
/**
 * Phone表的DAO实现
 */
public class PhoneImpl implements PhoneDAO {
    @Override
    public List<Phone> selectAllPhones() throws SQLException {
        return JdbcUtil.executeQuery(Phone.class,"select * from phone");
    }

    @Override
    public void insertPhone(Phone phone) throws SQLException {
        JdbcUtil.executeUpdate("insert into phone(id,inventory,name,type,price,date) values(?,?,?,?,?,?)",
                phone.getId(),phone.getInventory(),phone.getName(),phone.getType(),phone.getPrice(),phone.getDate());
    }

    @Override
    public void updatePhoneById(Phone phone) throws SQLException {
        JdbcUtil.executeUpdate("update phone set inventory=?,name=?,type=?,price=?,date=? where id=?",
                phone.getInventory(),phone.getName(),phone.getType(),phone.getPrice(),phone.getDate(),phone.getId());
    }

    @Override
    public Phone selectPhoneById(int id) throws SQLException {
        return JdbcUtil.queryOne(Phone.class,"select * from phone where id =?",id);
    }

    @Override
    public void deletePhoneById(int id) throws SQLException {
        JdbcUtil.executeUpdate("delete from phone where id =?",id);
    }
}
